<?php
global $types;

$options = $newoptions = get_option('tumblelog');

if ( isset($_POST['update']) ) { 
	check_admin_referer('theme-options');
	
	foreach ( $types as $type ) :
		$newoptions[$type] = $_POST[$type];
	endforeach;
		
	if ( $options != $newoptions ) {
		$options = $newoptions;
		
		update_option('tumblelog', $options);

		echo '<div id="message" class="updated fade"><p>Theme options have been updated.</p></div>';
	} 
} 
?>

<div class="wrap">

	<h2>Theme Options</h2>
	
	<form action="" method="post" id="config">
	<?php wp_nonce_field('theme-options') ?>

	<p>Assign a <strong>unique</strong> category to each type of post.</p>	
	
	<table class="form-table" style="width: 100%;" cellspacing="2" cellpadding="5">
	<?php 
		foreach ( $types as $type ) {
	?>
	<tr class="form-field">
		<th valign="top"  scope="row"><label for="<?php echo $type ?>"><?php echo ucwords($type) ?></label></th>
		<td>
		<select id="<?php echo $type ?>" name="<?php echo $type ?>">
			<option value="0" <?php selected($options[$type], '0') ?>>None</option>
	<?php
			foreach ( get_categories('get=all') as $category ) :     
				$category = sanitize_category($category) 
	?>
			<option value="<?php echo($category->term_id) ?>" <?php selected($options[$type], $category->term_id) ?>><?php echo($category->name) ?></option>
	<?php 
			endforeach; 
	?>
		</select>
		</td>
	</tr>
	<?php } ?>
	</table>

	<p class="submit">
		<input type="submit" name="update" class="update button" value="Update Options" />
	</p>
	</form>	

	<h3>Uninstall</h3>
	<form action="" method="post" id="uninstall">
	
	<p>Uninstalling reverts WordPress to the default theme and removes all theme settings from the database. No files are deleted. Perfect for if you want to start afresh.</p>
	
	<p class="submit">
		<input type="submit" name="uninstall" class="uninstall button" <?php echo "onClick=\"if ( confirm('" . js_escape(__("This will delete all this theme settings.\n'Cancel' to stop, 'OK' to delete.")) . "') ) { document.forms.post._wpnonce.value = '" . wp_create_nonce( 'theme-options' ) . "'; return true; } return false;\""; ?> value="Reset and Uninstall theme" />
	</p>
	</form>

</div>